﻿using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System.Collections;
using System.Configuration.Install;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace VPNMonitor
{
	static class Program
	{

		private static string ServiceName = "VPNMonitorService";

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
		{
			if (args.Length > 0)
			{
				if (args[0].ToUpper() == "/I")
				{
					InstallService();
				}
				else if (args[0].ToUpper() == "/U")
				{
					UninstallService();
				}
				else
				{
					MessageBox.Show("Неверный параметр. \"/i\" чтобы инсталлировать, \"/u\" чтобы деинсталлировать.", ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
			else
			{
				ServiceBase[] ServicesToRun;
				ServicesToRun = new ServiceBase[] 
				{ 
					new Service() 
				};
				ServiceBase.Run(ServicesToRun);
			}
		}

		private static void InstallService()
		{
			Hashtable SavedState = new Hashtable();
			AssemblyInstaller ai = new AssemblyInstaller(Assembly.GetExecutingAssembly().ManifestModule.Name, new string[] { });
			ai.AfterInstall += new InstallEventHandler(ai_AfterInstall);
			ai.Install(SavedState);
			ai.Commit(SavedState);
			MessageBox.Show("Сервис инсталлирован", ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		static void ai_AfterInstall(object sender, InstallEventArgs e)
		{
			ServiceController target = new ServiceController(ServiceName);
			switch (target.Status)
			{
				case ServiceControllerStatus.Running:
					break;

				case ServiceControllerStatus.Stopped:
					{
						Thread.Sleep(1500);

						target.Start();
						target.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 25));
						break;
					}
			}
		}

		private static void UninstallService()
		{
			Hashtable SavedState = new Hashtable();
			AssemblyInstaller ai = new AssemblyInstaller(Assembly.GetExecutingAssembly().ManifestModule.Name, new string[] { "/u" });
			StopService();
			Thread.Sleep(1000);
			ai.Uninstall(SavedState);
			//ai.Commit(SavedState);
			MessageBox.Show("Сервис деинсталлирован", ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		static void StopService()
		{
			ServiceController target = new ServiceController(ServiceName);
			switch (target.Status)
			{
				case ServiceControllerStatus.Running:
					{
						target.Stop();
						target.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
						if (target.Status != ServiceControllerStatus.Stopped)
						{
							Process[] ps = Process.GetProcessesByName(ServiceName);
							if (ps.Length > 0)
							{
								try
								{
									ps[0].Kill();
								}
								catch
								{

								}
							}
						}
						break;
					}

				case ServiceControllerStatus.Stopped:
					break;
			}
		}
	}
}
